package com.example.bourse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.bourse.core.result.PageResult;
import com.example.bourse.mapper.RoleMapper;
import com.example.bourse.pojo.entity.Menu;
import com.example.bourse.pojo.entity.Role;
import com.example.bourse.service.RoleService;
import org.apache.ibatis.solon.annotation.Db;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;

import java.util.List;

@Component
public class RoleServiceImpl implements RoleService {

    @Db
    private RoleMapper roleMapper;

    @Override
    public List<Role> getAllRoles() {
        return roleMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public long countRoles() {
        return roleMapper.selectCount(new QueryWrapper<>());
    }

    @Override
    public PageResult<Role> listRoles(int page, String roleName) {
        return null;
    }

    @Override
    public Role getRoleById(long id) {
        return roleMapper.selectById(id);
    }

    @Tran
    @Override
    public void addRole(Role role) {
        roleMapper.insert(role);
    }

    @Tran
    @Override
    public void updateRole(Role role) {
        roleMapper.updateById(role);
    }

    @Tran
    @Override
    public void deleteRole(long roleId) {
        roleMapper.deleteById(roleId);
    }

    @Override
    public List<Menu> getRoleMenus(long roleId) {
        return roleMapper.selectRoleMenus(roleId);
    }

    @Tran
    @Override
    public void saveRoleMenus(long roleId, List<Long> menuIds) {
        roleMapper.deleteRoleMenus(roleId);
        roleMapper.insertRoleMenus(roleId, menuIds);
    }
}
